package com.qiangqiang.resoldhouse.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.qiangqiang.resoldhouse.dto.AgentDTO;
import com.qiangqiang.resoldhouse.dto.EstateDTO;
import com.qiangqiang.resoldhouse.dto.HouseResourceDTO;
import com.qiangqiang.resoldhouse.dto.PageDTO;
import com.qiangqiang.resoldhouse.entity.*;
import com.qiangqiang.resoldhouse.service.AgentService;
import com.qiangqiang.resoldhouse.service.EstateService;
import com.qiangqiang.resoldhouse.service.HouseResourceService;
import com.qiangqiang.resoldhouse.util.ResultVOUtil;
import com.qiangqiang.resoldhouse.vo.ResultVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * @author: zqh
 * @date: 2018/8/7 16:22
 * @description:
 */
@Controller
public class HouseResourceController {

    @Autowired
    private HouseResourceService houseResourceService;

    @Autowired
    private AgentService agentService;

    @Autowired
    private EstateService estateService;

    //验证要修改的房源信息的经纪人与当前登陆的用户是否为同一个，否则跳转到错误页面
    @RequestMapping("/update_house_before")
    public String updateHouseBefore(@RequestParam Long houseId, Map<String, Object> map, HttpSession session){
        HouseResourceDTO house = houseResourceService.findHouseById(houseId);
        Agent agent = (Agent)session.getAttribute("agent");

        //经纪人未登录或房源不是由此经纪人发布的
        if(agent == null || house == null || house.getAgentId() != agent.getAgentId()){
            //返回错误页面
            return "error_update_house";
        }
        map.put("house", house);
        return "agentPage/update_house";
    }

    @RequestMapping("/update_house_after")
    public String updateHouseAfter(HouseResource house){
        System.out.println(house);
        houseResourceService.updateHouseResource(house);
        return "redirect:/user/center";
    }

    @RequestMapping("/delete_house")
    public String deleteHouseById(@RequestParam Long houseId){
        houseResourceService.deleteHouseResourse(houseId);
        return "redirect:/user/center";
    }

    //查看不到未审核的房源
    @RequestMapping("/house_list")
    public String getPage(@RequestParam(defaultValue = "1") Integer page, Map<String, Object> map, HouseResourceSelect select){
        if(select.getOrder() == null || select.getOrder().trim().equals("")) select.setOrder("default");
        if(select.getTitle() == null || select.getTitle().trim().equals("")) select.setTitle(null);
        if(select.getStatus() == null){
            Set<String> status = new HashSet<>();
            status.add("1");
            select.setStatus(status);
        }
        map.put("pageDTO", houseResourceService.findPage(page, select));
        map.put("select", select);
        return "house_list";
    }



    @RequestMapping("/house_detail")
    public String getDetails(@RequestParam("houseId") Long houseId, Map<String, Object> map){
        HouseResourceDTO house = houseResourceService.findHouseById(houseId);

        //经纪人字段可能没设置，直接返回null
        AgentDTO agent = null;
        if(house.getAgentId() != null)
             agent = agentService.findAgentDTOById(house.getAgentId());
        Estate estate = estateService.findEstateByEstateName(house.getEstateName());

        //数据没趴全，可能有没爬到的小区，这时候随便返回一个小区。。。
        if(estate == null) estate = estateService.findEstateByEstateName("南湖国际社区");
        if(agent == null) agent = agentService.findAgentDTOById(1L);
        map.put("house", house);
        map.put("agent", agent);
        map.put("estate", estate);
        return "house_detail";
    }


    @RequestMapping("/add_house_before")
    public String addHouseBefore(Map<String, Object> map, HttpSession session){

//        测试
//        Agent defaultAgent= agentService.findAgentByAccount("123");
//        session.setAttribute("agent", defaultAgent);
        Agent agent = (Agent)session.getAttribute("agent");
        if(agent == null) return "redirect:/house_list";

        //将经纪人id传到表单中一起提交
        map.put("agentId", agent.getAgentId());
        map.put("estateList", estateService.findAllEstate());
        return "add_house";
    }

    @RequestMapping("/add_house_after")
    public String addHouseAfter(Map<String, Object> map, HouseResourceDTO house, HttpSession session){
//        System.out.println(house);
        String city = estateService.findCityByEstateName(house.getEstateName());
        houseResourceService.insertHouseResourse(house, city);
        return "redirect:/user/center";
    }

    //后台房源页面
    @RequestMapping("/sys_house")
    public String getSysPage(@RequestParam(defaultValue = "1") Integer page, Map<String, Object> map, HouseResourceSelect select){
        if(select.getOrder() == null || select.getOrder().trim().equals("")) select.setOrder("default");
        if(select.getTitle() == null || select.getTitle().trim().equals("")) select.setTitle(null);
        map.put("pageDTO", houseResourceService.findPage(page, select));
        map.put("select", select);
        return "system/sys_house";
    }


    //后台删除房源
    @GetMapping("/sys_deletehouse")
    public String delete_syshouse(@RequestParam Long houseId){
        houseResourceService.deleteHouseResourse(houseId);
        return  "redirect:/sys_house";
    }

    //后台修改房源审核状态
    @GetMapping("/status_syshouse")
    public String status_syshouse(@RequestParam Long houseId,boolean status){
        houseResourceService.passHouse(houseId,status);
        return  "redirect:/sys_house";
    }

    //ajax请求房源列表,当经纪人查看自己发布的房源列表时，参数带上showAll=true，就能看到自己的所有房源信息了
    @ResponseBody
    @RequestMapping("/ajax_house_list")
    public Map getAjaxPage(Long agentId, String estateName, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "false") Boolean showAll){
        HouseResourceSelect select = new HouseResourceSelect();
        select.setAgentId(agentId);
        select.setEstateName(estateName);

        if(showAll == false) {
            //默认只能看到已过审核的
            Set<String> set = new HashSet<>();
            set.add("1");
            select.setStatus(set);
        }

//        System.out.println(select);
        Map<String, Object> map = new HashMap<>();
        PageDTO<HouseResourceDTO> pageDTO = houseResourceService.findPage(page, select);
        map.put("result", pageDTO.getResult());
        map.put("current", pageDTO.getPage().getCurrent());
        map.put("pages", pageDTO.getPage().getPages());
        map.put("hasPrePage", pageDTO.getPage().hasPrevious());
        map.put("hasNextPage", pageDTO.getPage().hasNext());
        map.put("total", pageDTO.getPage().getTotal());
        return map;
    }
}
